﻿import com.dmotiko.general.*
import com.dmotiko.mirador.*
import mx.transitions.easing.Regular;
import mx.transitions.Tween;

class MiradorLoader
extends BaseClip {	
	
	private var mcColor:MovieClip;
	private var mcMask:MovieClip;
	private var aParts:Array;
	private var lastPart:MovieClip;
		
	private function initClip():Void {
		mcColor.setMask(mcMask);
		aParts = [ mcMask.mc1, mcMask.mc2, mcMask.mc3, mcMask.mc4, mcMask.mc5, mcMask.mc6, mcMask.mc7, mcMask.mc8, mcMask.mc9, mcMask.mc10 ];
		for (var i:Number = 0; i < aParts.length; i++) {
			aParts[i].nPart = i+1;
			aParts[i]._alpha = 0;
		}
	}	
	
	public function setData(data:Object):Void {
		if (data > 100) data = 100;
		if ( data < 0) data = 0;
		super.setData( data );
	}
	
	private function refreshData():Void {
		var nPart:Number = Math.round(data / 10);
		if ( lastPart && lastPart.nPart >= nPart ) return;
		var clip:MovieClip = getNextPart(lastPart);
		if (!clip) clip = aParts[0];
		if (clip._alpha == 100) return;
		registerTween( "fadePart" + clip.nPart, new Tween( clip , "_alpha", Regular.easeOut, clip._alpha, 100, 0.2, true), false, clip.nPart==10, true);
	}
	
	private function tweenChanged( key:String, tween:Tween):Void {
		if( !tween["flag"] && tween.position > 50 ){
			tween["flag"] = true;
			var nPart:Number = Math.round(data / 10);
			lastPart = MovieClip(tween.obj);
			var next:MovieClip = getNextPart( lastPart );
			if (next.nPart > nPart) return;
			if (next) registerTween( "fadePart" + next.nPart, new Tween( next , "_alpha", Regular.easeOut, next._alpha, 100, 0.2, true), false, next.nPart == 10, true);
		}
	}
	
	private function tweenFinished( key:String, tween:Tween):Void {
		dispatchEvent( { type: BaseClipEvent.COMPLETE, target: this, data: data } );
	}
	
	private function getNextPart( part:MovieClip ):MovieClip {
		for (var i:Number = 0; i < aParts.length; i++) {
			if ( aParts[i] == part ) {
				return aParts[i + 1];
			}
		}
	}
	
	
}